package edu.zqu.collegeField.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.zqu.collegeField.common.utils.BeanMapper;
import edu.zqu.collegeField.framework.openfeign.api.GetCollegeNameApi;
import edu.zqu.collegeField.project.po.Field;
import edu.zqu.collegeField.project.po.FieldApplication;
import edu.zqu.collegeField.project.mapper.FieldApplicationMapper;
import edu.zqu.collegeField.project.service.FieldApplicationService;
import edu.zqu.collegeField.project.vo.FieldApplicationVo;
import edu.zqu.collegeField.project.vo.FieldVo;
import edu.zqu.common.core.text.Convert;
import edu.zqu.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 场地申请信息 服务实现类
 * </p>
 *
 * @author 廖森
 * @since 2020-06-28
 */
@Service
public class FieldApplicationServiceImpl extends ServiceImpl<FieldApplicationMapper, FieldApplication> implements FieldApplicationService {
    @Autowired
    private FieldApplicationMapper fieldApplicationMapper;
    @Autowired
    private GetCollegeNameApi getCollegeNameApi;
    @Override
    public List<FieldApplicationVo> get(BigInteger collegeId,//获取场地申请列表
                                        BigInteger applicantId,
                                        BigInteger fieldId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("collegeId", Convert.toStr(collegeId));
        map.put("applicantId", Convert.toStr(applicantId));
        map.put("fieldId", Convert.toStr(fieldId));
        List<FieldApplicationVo> fieldApplicationVoList = BeanMapper.mapList(fieldApplicationMapper.get(map), FieldApplicationVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(collegeId);
        for (FieldApplicationVo fieldApplicationVo : fieldApplicationVoList) {
            fieldApplicationVo.setCollegeName(collegeName);
        }
        return fieldApplicationVoList;
    }

    @Override
    public FieldApplicationVo getById(BigInteger id) {//获取场地申请
        HashMap<String, String> map = new HashMap<>();
        map.put("id", Convert.toStr(id));
        FieldApplicationVo fieldApplicationVo = BeanMapper.map(fieldApplicationMapper.getById(map), FieldApplicationVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(fieldApplicationVo.getCollegeId());
        fieldApplicationVo.setCollegeName(collegeName);
        return fieldApplicationVo;
    }

    @Override
    public int add(FieldApplication fieldApplication) {//添加场地申请
        fieldApplication.setId(null).setDeleted(null).setUpdateTime(new Timestamp(System.currentTimeMillis()));
        return fieldApplicationMapper.insert(fieldApplication);
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int update(FieldApplication fieldApplicationUpdate) {//修改场地申请
        FieldApplication fieldApplication = fieldApplicationMapper.selectById(fieldApplicationUpdate.getId());
        if (StringUtils.isNull(fieldApplication)) {
            throw new NullPointerException("找不到该场地申请");
        }
        try {
            BeanMapper.copyPropertiesNotNull(fieldApplicationUpdate, fieldApplication);
            int insert = fieldApplicationMapper.insert(fieldApplication.setLastVersion(fieldApplicationUpdate.getId()));
            if (insert >= 1) {
                return delete(fieldApplicationUpdate.getId(), fieldApplicationUpdate.getByWho());
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("更新失败");
        }
        return 0;
    }


    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int delete(BigInteger id, BigInteger byWho) {//删除场地申请
        try {
            UpdateWrapper<FieldApplication> fieldApplicationUpdateWrapper = new UpdateWrapper<FieldApplication>().eq("id", Convert.toStr(id));
            int update = fieldApplicationMapper.update(new FieldApplication().setByWho(byWho), fieldApplicationUpdateWrapper);
            if (update >= 1) {
                return fieldApplicationMapper.delete(fieldApplicationUpdateWrapper);
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("删除失败");
        }
        return 0;
    }
}
